import csv
import numpy as np
import pandas as pd
# from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA


def simplePCA(raw,dimen=1):                 #矩阵降维
	pca=PCA(n_components=dimen,copy=True)
	newData=pca.fit_transform(raw)
	return newData


def file2fullMatrix(filename):            #把csv文件制成矩阵
	with open(filename) as csvfile:
		spamReader=csv.reader(csvfile)
		listReader=np.array(list((spamReader)))[:,1:]
	

		height=len(listReader)
		width=len(listReader[1])

		# print(height)
		# print(width)


		feature=np.zeros((height-2,width-1))
		target=[]
		feature_name=listReader[1,:]
		for i in range(2,height):
			feature[i-2,]=listReader[i][:width-1]
			target.append(listReader[i][width-1])

		
		return feature,np.array(target),feature_name


def file2xMatrix(filename,num=2):        #只取前n个特征来完成矩阵,没什么用
	with open(filename) as csvfile:
		spamReader=csv.reader(csvfile)
		listReader=np.array(list((spamReader)))[:,1:]
	

		height=len(listReader)
		width=len(listReader[1])

		# print(height)
		# print(width)a


		feature=np.zeros((height-2,num))
		target=listReader[:,-1]
		feature_name=[listReader[1][i] for i in range(0,num)]
		
		for i in range(num):
			feature[:,i]=listReader[2:,i]
			# target.append(listReader[i][width-1])

		
		return feature,target,feature_name



def autoNorm(dataSet):                       #规格化数据
	minVals=dataSet.min(0)
	maxVals=dataSet.max(0)

	ranges=maxVals-minVals


	normDataSet=(dataSet-minVals)/ranges

	return normDataSet



